package zct.proj.util;

import java.util.ArrayList;

import zct.proj.equipModel.DCInfo;
import zct.proj.equipModel.DCModel;
import zct.proj.equipModel.DHANInfo;
import zct.proj.equipModel.DHANModel;
import zct.proj.equipModel.JYJInfo;
import zct.proj.equipModel.JYJModel;
import zct.proj.equipModel.WCQDInfo;
import zct.proj.equipModel.WCQDModel;
import zct.proj.equipModel.XHJInfo;
import zct.proj.equipModel.XHJModel;
import zct.proj.pojo.EquipDC;
import zct.proj.pojo.EquipXHJ;
import zct.proj.pojo.EquipmentInfo;
import zct.proj.pojo.StationSummary;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.Log;

public class DrawUtil {

	private int lineWeight = 2;
	private int tolWidth;
	private int tolHeight;
	public float xSpacing;
	public float ySpacing;
	private Canvas bk;
	private Paint strokePaint = new Paint();
	private Paint textPaint = new Paint();
	private Paint fillPaint = new Paint();
	private float curScale = 2;
	private Bitmap cacheBitmap;
	private Context ctx;
	public int mapWidth;
	public int mapHeight;
	public int baseXSpacing;
	public int baseYSpacing;
	private boolean drawing;
	
	private XHJModel xhjModel;
	private DHANModel dhanModel;
	private DCModel dcModel;
	private JYJModel jyjModel;
	private WCQDModel wcqdModel;
	
	private Path warnMark = null;
	
	public int getTolWidth() {
		return tolWidth;
	}
	
	public int getTolHeight() {
		return tolHeight;
	}
	
	public float getScale() {
		return curScale;
	}
	
	public void setScaleAndDraw(float scale, ArrayList<EquipmentInfo> eiList) {
		// 根据新宽度设置缩放比例，每次缩放向上放大至10%
		float scl = 0.1f * ((int) (scale * 10) + 1);
		if (scl > Const.MAX_ZOOM_RATE) {
			scl = Const.MAX_ZOOM_RATE;
		}
		if (curScale == scl) {
			return;
		}
//		if (scl == 0) {
//			scl = 0;
//		}
//		Log.i("drawutil setscale", scl + "");
		initVars(scl);
		drawControls(eiList);
	}
	
	public void zoom(float value) {
		curScale *= value;
	}
	
	public Bitmap getCacheBitmap() {
		return cacheBitmap;
	}
	
	public DrawUtil(Context context, StationSummary ss, float scale) {
		ctx = context;
		mapWidth = ss.getScreenWidth();
		mapHeight = ss.getScreenHeight();
		lineWeight = ss.getLineWeight();
		baseXSpacing = ss.getXSpacing();
		baseYSpacing = ss.getYSpacing();
		initVars(scale);
	}
	
	private void initVars(float scale) {
		curScale = scale;
		tolWidth = Math.round (curScale * mapWidth);
		tolHeight = Math.round (curScale * mapHeight);
		
		xSpacing = (curScale * baseXSpacing);
		ySpacing = (curScale * baseYSpacing);
		
		xhjModel = new XHJModel(curScale, baseXSpacing, baseYSpacing);
		dhanModel = new DHANModel(curScale, baseXSpacing, baseYSpacing);
		dcModel = new DCModel(curScale, baseXSpacing, baseYSpacing);
		jyjModel = new JYJModel(curScale, baseXSpacing, baseYSpacing);
		wcqdModel = new WCQDModel(curScale, baseXSpacing, baseYSpacing);
		
		strokePaint.setStyle(Style.STROKE);
		strokePaint.setStrokeWidth(lineWeight);
		strokePaint.setAntiAlias(false);
		strokePaint.setColor(Color.WHITE);
		
		textPaint.setColor(Color.WHITE);
		textPaint.setStrokeWidth(1);
		textPaint.setTypeface(Typeface.DEFAULT);
		textPaint.setAntiAlias(true);
		
		fillPaint.setAntiAlias(true);
		fillPaint.setStyle(Style.FILL);
		
		// 生成全尺寸位图
//		if (cacheBitmap != null) {
//			cacheBitmap.recycle();
//			cacheBitmap = null;
//			synchronized (bk) {
//				
//			}
//		}
//		if (Math.round (tolWidth + xSpacing * 2) == 0) {
//			Log.i("DUtil", Math.round (tolWidth + xSpacing * 2) + "," + Math.round (tolHeight + ySpacing * 2));
//		}
//		if (cacheBitmap != null) {
////			cacheBitmap.recycle();
//			cacheBitmap = null;
//		}
		try {
			cacheBitmap = Bitmap.createBitmap(Math.round (tolWidth + xSpacing * 2), Math.round (tolHeight + ySpacing * 2), Config.RGB_565);
		} catch (Exception e) {
			e.printStackTrace();
		}
		bk = new Canvas(cacheBitmap);
		// 背景色
		bk.drawColor(Color.BLACK);
	}
	
	public void redrawAll(ArrayList<EquipmentInfo> detailList) {
		if (drawing) {
			return;
		}
		drawing = true;
		bk.drawColor(Color.BLACK);
		for (EquipmentInfo info : detailList) {
			drawControl(info);
		}
		drawing = false;
	}
	
	/**
	 * 绘制所有控件
	 * @param detailList
	 */
	public void drawControls(ArrayList<EquipmentInfo> detailList) {
		if (drawing) {
			return;
		}
		drawing = true;
		for (EquipmentInfo info : detailList) {
			drawControl(info);
		}
		drawing = false;
	}
	
	/**
	 * 绘制单个控件
	 * @param detail
	 */
	private void drawControl(EquipmentInfo detail) {

		if (Const.EQUI_XHJ.equals(detail.getName())) {
			// 信号机
			drawSbXHJ(detail);
		} else if (Const.EQUI_TEXT.equals(detail.getName())) {
			// 文本
			drawSbWB(detail);
		} else if (Const.EQUI_DHAN.equals(detail.getName())) {
			// 灯和按钮
			drawSbDHAN(detail);
		} else if (Const.EQUI_DC.equals(detail.getName())) {
			// 道岔
//			drawSbDC((EquipDC) detail);
		} else if (Const.EQUI_JYJ.equals(detail.getName())) {
			// 绝缘节
			drawSbJYJ(detail);
		} else if (Const.EQUI_WCQD.equals(detail.getName())) {
			// 无岔区段
			drawSbWCQD(detail);
		}
	}
	
	/**
	 * 无岔区段
	 * @param detail
	 */
	private void drawSbWCQD(EquipmentInfo detail) {
//		if (detail.getDisp().equals("D10G")) {
//			Log.i("aaa","");
//		}
		WCQDInfo wInfo = wcqdModel.getWCQDInfo(detail);
		Point ptBase = new Point(Math.round (detail.getX() * xSpacing), Math.round (detail.getY() * ySpacing));
		
		Path dp = new Path(wInfo.skeleton);
		dp.offset(ptBase.x, ptBase.y);
		drawPath(dp, getStrokeWidth(), detail.getColor());
		
		int scLeft = Math.round (detail.getX() * xSpacing);
		int scTop = Math.round (detail.getY() * ySpacing);
		if (detail.getNameVisible() && detail.getDisp() != null && !"".equals(detail.getDisp().trim())) {
			try {
				drawText(detail.getDisp(), detail.getFont(), detail.getFontsize(), 
						wInfo.getTextPos().x + scLeft, wInfo.getTextPos().y + scTop, 
						detail.getTextColor(), wInfo.getTextAlign());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void drawSbWCQD(EquipmentInfo detail, int color, Canvas canvas) {
		WCQDInfo wInfo = wcqdModel.getWCQDInfo(detail);
		
		Point ptBase = new Point(Math.round (detail.getX() * xSpacing), Math.round (detail.getY() * ySpacing));
		Path dp = new Path(wInfo.skeleton);
		dp.offset(ptBase.x, ptBase.y);
//		drawPath(dp, 2, detail.getColor());
		strokePaint.setStrokeWidth(getStrokeWidth());
		strokePaint.setColor(color);
		canvas.drawPath(dp, strokePaint);
		
		
//		if (detail.getX() <50) {
//			Paint paint = new Paint();
//			paint.setStrokeWidth(1);
//			paint.setStyle(Style.STROKE);
//			paint.setColor(Color.RED);
//			PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);  
//			paint.setPathEffect(effects);
//			Rect drawRect = new Rect(wInfo.rect);
//			drawRect.offset(ptBase.x, ptBase.y);
//			canvas.drawRect(drawRect, paint);
//			Log.i("drawUtil", "rect : " + drawRect.left + "," + drawRect.top + "," + drawRect.right + "," + drawRect.bottom);
//		}
	}
	
	public boolean touchedInControll(EquipmentInfo detail, int orgX, int orgY) {
		
		WCQDInfo wInfo = wcqdModel.getWCQDInfo(detail);
//		RectF r = new RectF();
//		wInfo.skeleton.computeBounds(r, false);
		// 可点区域扩大10像素
		Rect r = new Rect(wInfo.rect.left, wInfo.rect.top, wInfo.rect.right, wInfo.rect.bottom);
		if ((detail.getType() == Const.TYPE_WCQD_XHZX
				|| detail.getType() == Const.TYPE_WCQD_WXHZX)
				&& (detail.getDir() == Const.DIR_NORMAL || detail.getDir() == Const.DIR_ROTATE)) {
			r = new Rect(wInfo.rect.left - 20, wInfo.rect.top - 20, wInfo.rect.right + 20, wInfo.rect.bottom + 20);
		}
//		r.top -= baseYSpacing;
//		r.bottom += baseYSpacing;
//		r.left -= baseXSpacing;
//		r.right += baseXSpacing;
		Point ptBase = new Point(Math.round (detail.getX() * xSpacing), Math.round (detail.getY() * ySpacing));
		r.offset(ptBase.x, ptBase.y);
		if (r.contains((int) (curScale * orgX), (int) (curScale * orgY))) {
			Log.i("DrawUtil", "found match equip @ " + orgX + "," + orgY+",r.l:"+r.left/curScale+",r.r:"+r.right/curScale+",r.t:"+r.top/curScale+",r.b:"+r.bottom/curScale);
			return true;
		}
		return false;
	}
	
	public void drawSbWCQDRect(EquipmentInfo detail, Canvas canvas, int color) {
		WCQDInfo wInfo = wcqdModel.getWCQDInfo(detail);
		Point ptBase = new Point(Math.round (detail.getX() * xSpacing), Math.round (detail.getY() * ySpacing));
		
		Paint paint = new Paint();
		paint.setStrokeWidth(1);
		paint.setStyle(Style.STROKE);
		paint.setColor(color);
		PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);  
		paint.setPathEffect(effects);
		Rect drawRect = new Rect(wInfo.rect);
		drawRect.offset(ptBase.x, ptBase.y);
		canvas.drawRect(drawRect, paint);
//		canvas.drawCircle(100, 100, 100, paint);
//		Log.i("drawUtil", "drawSbWCQDRect : " + drawRect.left + "," + drawRect.top + "," + drawRect.right + "," + drawRect.bottom);
//		Log.i("drawUtil", "equip:" + detail.getX() + "," + xSpacing);
	}
	
	public void drawSbWCQDMark(EquipmentInfo detail, Canvas canvas, int color) {
		WCQDInfo wInfo = wcqdModel.getWCQDInfo(detail);
		Point ptBase = new Point(Math.round (detail.getX() * xSpacing), Math.round (detail.getY() * ySpacing));
		
		if (warnMark == null) {
			warnMark = new Path();
			warnMark.moveTo(0, 0);
			warnMark.lineTo(10, 0);
			warnMark.lineTo(5, 20);
			warnMark.close();
			warnMark.offset(-5, -20);
		}
		
		Paint paint = new Paint();
		paint.setStrokeWidth(1);
//		paint.setStyle(Style.STROKE);
		paint.setColor(color);
//		PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);  
//		paint.setPathEffect(effects);
		Point drawAt = new Point();
		drawAt.x = (wInfo.rect.left + wInfo.rect.right) / 2;
		drawAt.y = (wInfo.rect.top + wInfo.rect.bottom) / 2;
		drawAt.offset(ptBase.x, ptBase.y);
		Path drawPath = new Path(warnMark);
		drawPath.offset(drawAt.x, drawAt.y);
//		Rect drawRect = new Rect(wInfo.rect);
//		drawRect.offset(ptBase.x, ptBase.y);
		canvas.drawPath(drawPath, paint);
//		canvas.drawCircle(100, 100, 100, paint);
//		Log.i("drawUtil", "drawSbWCQDRect : " + drawRect.left + "," + drawRect.top + "," + drawRect.right + "," + drawRect.bottom);
//		Log.i("drawUtil", "equip:" + detail.getX() + "," + xSpacing);
	}
	
//	public Rect drawSbWCQDRect(EquipmentInfo detail, Canvas canvas, Rect bgRect) {
//		WCQDInfo wInfo = wcqdModel.getWCQDInfo(detail);
//		
////		int offx = touchX- ptBase.x;
////		int offy = touchY - ptBase.y;
////		if (wInfo.rect.contains(offx, offy)) {
//			Paint paint = new Paint();
//			paint.setStrokeWidth(1);
//			paint.setStyle(Style.STROKE);
//			paint.setColor(Color.YELLOW);
//			PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);  
//			paint.setPathEffect(effects);
//			Rect drawRect = new Rect(wInfo.rect);
////			wInfo.skeleton.computeBounds(drawRect, false);
//			float scale = (float) bgRect.width() / mapWidth;
//			Point ptBase = new Point(Math.round ((int) (scale * xSpacing) * detail.getX() + bgRect.left),
//					Math.round ((int) (scale * ySpacing) * detail.getY()) + bgRect.top);
////			drawRect.offset(ptBase.x, ptBase.y);
//			
//			Rect newRect = new Rect((int) (scale * drawRect.left),
//					(int) (scale * drawRect.top),
//					(int) (scale * drawRect.right),
//					(int) (scale * drawRect.bottom));
//			newRect.offset(ptBase.x, ptBase.y);
//			Log.i("drawUtil", "draw rectF : " + newRect.left + "," + newRect.top + "," + newRect.right + "," + newRect.bottom);
//			canvas.drawRect(newRect, paint);
////			return true;
////		}
////		return false;
//			return wInfo.rect;
//	}
	
	/**
	 * 绝缘节
	 * @param detail
	 */
	private void drawSbJYJ(EquipmentInfo detail) {
		JYJInfo jInfo = jyjModel.getJYJInfo(detail);
		Point ptBase = new Point(Math.round (detail.getX() * xSpacing), Math.round (detail.getY() * ySpacing));
		Point p1 = new Point(jInfo.pt1);
		p1.offset(ptBase.x, ptBase.y);
		Point p2 = new Point(jInfo.pt2);
		p2.offset(ptBase.x, ptBase.y);
		drawLine(p1, p2, 2, detail.getColor());
		if (detail.getType() == Const.TYPE_JYJ_CX) {
			strokePaint.setColor(Color.RED);
			Point pc = new Point(jInfo.center);
			pc.offset(Math.round (detail.getX() * xSpacing), Math.round (detail.getY() * ySpacing));
			int wDis = Math.round (detail.getWidth() * xSpacing / 2);
			int hDis = Math.round (detail.getHeight() * ySpacing / 2);
			bk.drawOval(new RectF(pc.x - wDis, pc.y - hDis, pc.x + wDis, pc.y + hDis), strokePaint);
		}
	}
	
	/**
	 * 道岔绘图
	 * @param detail 信号机详细
	 */
//	private void drawSbDC(EquipDC detail) {
//		
//		DCInfo dInfo = dcModel.getDCInfo(detail);
//		strokePaint.setStrokeWidth(2);
//		Path c = null;
//		Path d = null;
//		
//		if (detail.getStatus() == Const.SW_DC_CINDEX_FIX) {
//			c = new Path(dInfo.direct1);
//			d = new Path(dInfo.curve1);
//		} else {
//			c = new Path(dInfo.direct2);
//			d = new Path(dInfo.curve2);
//		}
//		
//		
//		d.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
//		bk.drawPath(d, strokePaint);
//		c.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
//		if (dInfo.dirAfter != null) {
//			c = new Path(dInfo.dirAfter);
//			c.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
//		}
//		bk.drawPath(c, strokePaint);
//	}
	
	/**
	 * 信号机绘图
	 * @param detail 信号机详细
	 */
	private void drawSbXHJ(EquipmentInfo detail) {
		XHJInfo xInfo = xhjModel.getXHJInfo(detail);
		Path dp = new Path(xInfo.getSkeleton());
		dp.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
		strokePaint.setStrokeWidth(2);
		bk.drawPath(dp, strokePaint);
		int scLeft = Math.round (detail.getX() * xSpacing);
		int scTop = Math.round (detail.getY() * ySpacing);
		if (detail.getNameVisible() && detail.getDisp() != null && !"".equals(detail.getDisp().trim())) {
			try {
				drawText(detail.getDisp(), detail.getFont(), detail.getFontsize(), 
						xInfo.getTextPos().x + scLeft, xInfo.getTextPos().y + scTop, 
						detail.getTextColor(), xInfo.getTextAlign());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void drawSbXHJLamp1(EquipXHJ detail, int color, Canvas canvas) {
		XHJInfo dInfo = xhjModel.getXHJInfo(detail);
		fillPaint.setColor(color);
		Point pt = dInfo.getPoint();
		canvas.drawCircle(pt.x + detail.getX() * xSpacing, pt.y + detail.getY() * ySpacing, dhanModel.getRadius(), fillPaint);
	}
	
	public void drawSbXHJLamp2(EquipXHJ detail, int color, Canvas canvas) {
		XHJInfo dInfo = xhjModel.getXHJInfo(detail);
		fillPaint.setColor(color);
		Point pt = dInfo.getPoint2();
		canvas.drawCircle(pt.x + detail.getX() * xSpacing, pt.y + detail.getY() * ySpacing, dhanModel.getRadius(), fillPaint);
	}
	
	public void drawSbDC(EquipDC detail, int color, Canvas canvas) {
		DCInfo dInfo = dcModel.getDCInfo(detail);
		strokePaint.setStrokeWidth(getStrokeWidth());
		Path c = null;
		Path d = null;
		int dColor = Const.COLOR_DEACTIVE;
		int cColor = Const.COLOR_DEACTIVE;

		if (detail.getType() == Const.TYPE_DC_CZXDCZ 
				|| detail.getType() == Const.TYPE_DC_DDCZ
				|| detail.getType() == Const.TYPE_DC_XDCZ) {
			// 定位为直
//		if (detail.getDisp().equals("4") || detail.getDisp().equals("2")) {
//			Log.i("draw dc", "dc"+detail.getDisp()+":"+detail.getStatus()+"");
//		}
			if (detail.getStatus() == Const.SW_DC_CINDEX_OPP) {
				d = new Path(dInfo.direct2);
				c = new Path(dInfo.curve2);
				cColor = color;
			} else {
				d = new Path(dInfo.direct1);
				c = new Path(dInfo.curve1);
				dColor = color;
			}
		} else {
			// 定位为弯
			if (detail.getStatus() == Const.SW_DC_CINDEX_OPP) {
				d = new Path(dInfo.direct1);
				c = new Path(dInfo.curve1);
				dColor = color;
			} else {
				d = new Path(dInfo.direct2);
				c = new Path(dInfo.curve2);
				cColor = color;
			}
		}
		
		d.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
		strokePaint.setColor(dColor);
		canvas.drawPath(d, strokePaint);
		c.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
		if (dInfo.dirAfter != null) {
			c = new Path(dInfo.dirAfter);
			c.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
		}
		strokePaint.setColor(cColor);
		canvas.drawPath(c, strokePaint);
		
		int scLeft = Math.round (detail.getX() * xSpacing);
		int scTop = Math.round (detail.getY() * ySpacing);
		if (detail.getNameVisible() && detail.getDisp() != null && !"".equals(detail.getDisp().trim())) {
			try {
				drawText(detail.getDisp(), detail.getFont(), detail.getFontsize(), 
						dInfo.getTextPos().x + scLeft, dInfo.getTextPos().y + scTop, 
						detail.getTextColor(), dInfo.getTextAlign(), canvas);
//				Log.v("du", "drawSbName................................");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 文本显示
	 * @param detail
	 */
	private void drawSbWB(EquipmentInfo detail) {
		
		int size = Math.round (curScale * detail.getFontsize() * Const.WB_ZOOM_OUT);
		float x = (detail.getX() + detail.getWidth() / 2) * xSpacing;
		float y = detail.getY() * ySpacing + Math.round (size * 0.8);
		Point p = new Point(Math.round (x), Math.round (y));
		drawText(detail.getDisp(), detail.getFont(), size, p, detail.getTextColor(), Align.CENTER);
	}
	
	/**
	 * 等和按钮
	 * @param detail
	 */
	private void drawSbDHAN(EquipmentInfo detail) {
		
		DHANInfo dInfo = dhanModel.getDHANInfo(detail);
		if (detail.getType() == Const.TYPE_BTN_SQ) {
			strokePaint.setStrokeWidth(4);
		} else {
			strokePaint.setStrokeWidth(2);
		}
		int scLeft = Math.round (detail.getX() * xSpacing);
		int scTop = Math.round (detail.getY() * ySpacing);
		if (detail.getType() == Const.TYPE_BTN_LCAN
				|| detail.getType() == Const.TYPE_BTN_DCBS
				|| detail.getType() == Const.TYPE_BTN_DCAN
				|| detail.getType() == Const.TYPE_BTN_DQ
				|| detail.getType() == Const.TYPE_BTN_SQ) {
			// 灯或按钮 列车按钮  道岔表示  调车按钮  单圈   双圈
			Point pt = dInfo.getPoint();
			bk.drawCircle(pt.x + scLeft, pt.y + scTop, dInfo.getRaidus(), strokePaint);
		} else if (detail.getType() == Const.TYPE_BTN_FX) {
			// 灯或按钮 方形
			Point pt = dInfo.getPoint();
			bk.drawRect(pt.x + scLeft, pt.y + scTop, pt.x + scLeft + dInfo.getRaidus(), pt.y + scTop + dInfo.getRaidus(), strokePaint);
		} else if (detail.getType() == Const.TYPE_BTN_ARROW || detail.getType() == Const.TYPE_BTN_HALFARROR) {
			// 灯或按钮 箭头
			Path dp = new Path(dInfo.getSkeleton());
			dp.offset(scLeft, scTop);
			bk.drawPath(dp, strokePaint);
		}

		// 调车按钮不显示文本（20120527）
		if (detail.getType() != Const.TYPE_BTN_DCAN) {
			if (detail.getNameVisible() && detail.getDisp() != null && !"".equals(detail.getDisp().trim())) {
				try {
				drawText(detail.getDisp(), detail.getFont(), detail.getFontsize(), 
						dInfo.getTextPos().x + scLeft, dInfo.getTextPos().y + scTop, 
						detail.getTextColor(), dInfo.getTextAlign());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	public void drawSbDHANLamp(EquipmentInfo detail, int color, Canvas canvas) {
		
		DHANInfo dInfo = dhanModel.getDHANInfo(detail);
		fillPaint.setColor(color);
		Point pt = dInfo.getPoint();
//		try {
//		canvas.drawCircle(pt.x + detail.getX() * xSpacing, pt.y + detail.getY() * ySpacing, 10, fillPaint);
		
		switch (detail.getType()) {
		case Const.TYPE_BTN_ARROW:
			Path arrow = dhanModel.getArrowPath(detail.getWidth(), detail.getHeight(), detail.getDir());
			Path dp = new Path(arrow);
			dp.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
			canvas.drawPath(dp, fillPaint);
			break;
		case Const.TYPE_BTN_HALFARROR:
			Path halfArrow = dhanModel.getHalfArrowPath(detail.getWidth(), detail.getHeight(), detail.getDir());
			Path dph = new Path(halfArrow);
			dph.offset(detail.getX() * xSpacing, detail.getY() * ySpacing);
			canvas.drawPath(dph, fillPaint);
			break;
		case Const.TYPE_BTN_SQ:
			strokePaint.setStrokeWidth(4);
			strokePaint.setColor(color);
			canvas.drawCircle(pt.x + detail.getX() * xSpacing, pt.y + detail.getY() * ySpacing, dhanModel.getRadius(), strokePaint);
			strokePaint.setStrokeWidth(2);
			break;
		default:
			canvas.drawCircle(pt.x + detail.getX() * xSpacing, pt.y + detail.getY() * ySpacing, dhanModel.getRadius(), fillPaint);
			break;
		}
//		canvas.drawCircle(200, 200, 50, fillPaint);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}
	public void drawSbDHANLamp(EquipmentInfo detail, int color) {
		DHANInfo dInfo = dhanModel.getDHANInfo(detail);
		fillPaint.setColor(color);
		Point pt = dInfo.getPoint();
		bk.drawCircle(pt.x + detail.getX() * xSpacing, pt.y + detail.getY() * ySpacing, dhanModel.getRadius(), fillPaint);
	}
	
//	private void drawRing(Point p, int radius, int lineWeight, int color) {
//		drawRing(p.x, p.y, radius, lineWeight, color);
//	}
	
//	private void drawRing(int x, int y, int radius, int weight, int color) {
//		strokePaint.setColor(color);
//		strokePaint.setStrokeWidth(weight);
//		bk.drawCircle(x, y, radius, strokePaint);
//	}
	
	private void drawLine(Point pFrom, Point pTo, int lineWeight, int color) {
		strokePaint.setStrokeWidth(lineWeight);
		strokePaint.setColor(color);
		bk.drawLine(pFrom.x, pFrom.y, pTo.x, pTo.y, strokePaint);
	}
	
	private void drawText(String text, String fontName, int fontsize, Point p, int color, Align align) {
		try {
			drawText(text, fontName, fontsize, p.x, p.y, color, align);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void drawPath(Path p, int lineWeight, int color) {
		strokePaint.setStrokeWidth(lineWeight);
		strokePaint.setColor(color);
		bk.drawPath(p, strokePaint);
	}
	
	private void drawText(String text, String fontName, int fontsize, int x, int y, int color, Align align) {
		textPaint.setTextAlign(align);
		textPaint.setTypeface(FontUtil.getTypeface(ctx, fontName));
		textPaint.setColor(color);
		int minSize = 0;
		if (fontName.equals(Const.FONT_NAME_KAI)) {
			// 汉子最小字号10
			minSize = (int) Math.max(fontsize * curScale * 0.3 + 10, Const.MIN_FONT_SIZE_CHN);
		} else {
			minSize = (int) Math.max(fontsize * curScale * 0.3 + 10, Const.MIN_FONT_SIZE);
		}
		textPaint.setTextSize(minSize);
		bk.drawText(text, x, y, textPaint);
	}
	
	private void drawText(String text, String fontName, int fontsize, int x, int y, int color, Align align, Canvas canvas) {
		textPaint.setTextAlign(align);
		textPaint.setTypeface(FontUtil.getTypeface(ctx, fontName));
		textPaint.setColor(color);
		int minSize = 0;
		if (fontName.equals(Const.FONT_NAME_KAI)) {
			// 汉子最小字号10
			minSize = (int) Math.max(fontsize * curScale * 0.3 + 10, Const.MIN_FONT_SIZE_CHN);
		} else {
			minSize = (int) Math.max(fontsize * curScale * 0.3 + 10, Const.MIN_FONT_SIZE);
		}
		textPaint.setTextSize(minSize);
		canvas.drawText(text, x, y, textPaint);
	}
	
	/**
	 * 绘制标尺
	 */
	public void drawNet() {
//    	Paint paint = new Paint();
//    	paint.setColor(Color.GRAY);
//    	paint.setStrokeWidth(1);
//    	int cnt = 0;
//    	for (int x = xSpacing, maxX = tolWidth + xSpacing; x <= maxX; x+=xSpacing) {
//    		cnt++;
//    		for (int y = ySpacing, maxY = tolHeight + ySpacing; y <= maxY; y+=ySpacing) {
//    			bk.drawPoint(x, y, paint);
//    		}
//    	}
//    	Log.i("draw net", "total x point:" + cnt);
	}
	
	private int getStrokeWidth() {
		if (curScale >= 0.9) {
			return 3;
		}
		return 2;
	}
}
